#ifdef BUILD_WITH_FACEBOOK

#include "FacebookLoginDialog.h"

FacebookLoginDialog::FacebookLoginDialog(QWidget* parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
	ui.setupUi(this);

	connect(this, SIGNAL(done()), parent, SLOT(closeFbDialog()));

	QObject::connect(ui.webView, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
	connect(ui.webView, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int)));
	connect(ui.webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
	connect(ui.webView, SIGNAL(statusBarMessage(const QString&)), this, SLOT(message(const QString&)));
	connect(ui.webView, SIGNAL(urlChanged(const QUrl&)), this, SLOT(urlDidChange(const QUrl&)));

	m_cookieJar = new CustomCookieJar;
	ui.webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);

//	qDebug() << "FacebookLoginDialog::FacebookLoginDialog(); (this=" << unsigned int(this) << ");";
}

FacebookLoginDialog::~FacebookLoginDialog()
{
//	qDebug() << "FacebookLoginDialog::~FacebookLoginDialog(); (this=" << unsigned int(this) << ")";
}

void FacebookLoginDialog::loadProgress(int p)
{
	qDebug() << "Progress: " << p << "%...";
}

void FacebookLoginDialog::loadStarted()
{
	qDebug() << "Loading started.";
}

void FacebookLoginDialog::loadFinished(bool ok)
{
	qDebug() << "Loading ended. (" << (ok ? "ok" : "failed") << ")";

	QNetworkCookieJar* cookies = ui.webView->page()->networkAccessManager()->cookieJar();
}

void FacebookLoginDialog::message(const QString& msg)
{
	qDebug() << "Message: " << msg;
}

void FacebookLoginDialog::initAndShow()
{
	init();
	show();
}

void FacebookLoginDialog::init()
{
	QUrl url("http://www.facebook.com/login.php");
	url.addQueryItem("api_key", "87c0a992da2e73b7990ef3e41d63ec5d");
	url.addQueryItem("fbconnect", "1");
	url.addQueryItem("v", "1.0");
	url.addQueryItem("connect_display", "popup");
	url.addQueryItem("return_session", "true");
	//url.addQueryItem("session_key_only", "true");
	url.addQueryItem("skipcookie", "true");
	url.addQueryItem("next", "http://www.facebook.com/connect/login_success.html");
	url.addQueryItem("cancel_url", "http://www.facebook.com/connect/login_failure.html");
	url.addQueryItem("req_perms", "publish_stream");

	qDebug() << "URL Built: " << url.toString();

	ui.webView->load(url);
}

QString getValue(QString line, QString key)
{
	QString out;

	int pos = line.indexOf(key);
	if(pos >= 0)
	{
		pos += key.length() + 3;
		int endPos = 0;

		for(int i = pos; i < line.length(); ++i)
		{
			if(line[i] == '\"')
			{
				endPos = i;
				break;
			}
		}

		out = line.mid(pos, endPos - pos); 
	}

	return out;
}

void FacebookLoginDialog::urlDidChange(const QUrl& url)
{
	QList<QPair<QString, QString> > attrs = url.queryItems();
	
	QListIterator<QPair<QString, QString> > it(attrs);
	while(it.hasNext())
	{
		const QPair<QString, QString>& pair = it.next();
		qDebug() << "\t" << pair.first << ": " << pair.second << "\n";
		if(pair.first == "session")
		{
			m_sessionKey = getValue(pair.second, "session_key");
			m_uid = getValue(pair.second, "uid");
			m_secret = getValue(pair.second, "secret");
		}
	}
}

void FacebookLoginDialog::hideEvent(QHideEvent* event)
{
	emit done();
}

void FacebookLoginDialog::closeEvent(QCloseEvent *event)
{

}

#endif